import Vue from 'vue'
import VueRouter from 'vue-router'
import LayOut from '_views/LayOut'
import store from '../store'
import { isLogin } from '_utils/token'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'LayOut',
    component: LayOut,
    children: [
      {
        path: '/',
        redirect: '/dashBoard'
      },
      {
        path: '/dashBoard',
        name: '仪表盘',
        roles: ['*'],
        component: () => import('_views/DashBoard')
      },
      {
        path: '/spingTongji',
        name: '商品统计',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/SpingTongji')
      },
      {
        path: '/userTongji',
        name: '用户统计',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/UserTongji')
      },
      {
        path: '/jiaoyiTongji',
        name: '交易统计',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/JiaoyiTongji')
      },
      {
        path: '/cartLists',
        name: '商品分类',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/CartLists')
      },
      {
        path: '/itemLists',
        name: '商品管理',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/ItemLists')
      },
      {
        path: '/itemAdd',
        name: '增加商品',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/ItemAdd')
      },
      {
        path: '/itemPing',
        name: '商品评论',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/ItemPing')
      },
      {
        path: '/itemUpdate',
        name: '修改商品',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/ItemUpdate')
      },
      {
        path: '/dingDan',
        name: '订单',
        component: () => import('_views/DingDan')
      },
      {
        path: '/userInfo',
        name: '个人中心',
        component: () => import('_views/UserInfo')
      },
      {
        path: '/userBoqian',
        name: '用户标签',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/UserBoqian')
      },
      {
        path: '/userDengji',
        name: '用户等级',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/UserDengji')
      },
      {
        path: '/userFenzu',
        name: '用户分组',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/UserFenzu')
      },
      {
        path: '/wenZhanggl',
        name: '文章管理',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/WenZhanggl')
      }, {
        path: '/wenZhangnr',
        name: '文章内容',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/WenZhangnr')
      },
      {
        path: '/jifenPz',
        name: '积分配置',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/JiFen/JifenPz')
      },
      {
        path: '/jifenRz',
        roles: ['管理员', '普通员工', '大boss'],
        name: '积分日志',
        component: () => import('_views/YingXiao/JiFen/JifenRz')
      },
      {
        path: '/mxPz',
        name: '秒杀配置',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/MiaoXia/MxPz')
      },
      {
        path: '/mxSp',
        name: '秒杀商品',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/MiaoXia/MxSp')
      },
      {
        path: '/kanJialb',
        name: '砍价列表',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/KJ/KanJialb')
      },
      {
        path: '/kanJiasp',
        name: '秒杀商品',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/KJ/KanJiasp')
      },
      {
        path: '/pingTlb',
        name: '拼团列表',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/PingT/PingTlb')
      },
      {
        path: '/pingTsp',
        name: '拼团商品',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/PingT/PingTsp')
      },
      {
        path: '/caogaoLb',
        name: '草稿列表',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/videoGl/CaogaoLb')
      },
      {
        path: '/caogaoSp',
        name: '草稿列表商品',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/videoGl/CaogaoSp')
      },
      {
        path: '/yhQuan',
        name: '优惠券',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/YouHui/YhQuan')
      },
      {
        path: '/getHistory',
        name: '领取记录',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/YingXiao/YouHui/GetHistory')
      },
      {
        path: '/fenXiaopz',
        name: '分销配置',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/FenXiao/FenXiaopz')
      },
      {
        path: '/fenXiaoygl',
        name: '分销员配置',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/FenXiao/FenXiaoygl')
      },
      {
        path: '/senQintx',
        name: '申请提现',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/CaiWu/CaiwuCz/SenQintx')
      },
      {
        path: '/czJl',
        name: '充值记录',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/CaiWu/CaiwuJl/CzJl')
      },
      {
        path: '/zjingJk',
        name: '资金监控',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/CaiWu/CaiwuJl/ZjingJk')
      },
      {
        path: '/yjingJl',
        name: '佣金记录',
        roles: ['管理员', '普通员工', '大boss'],
        component: () => import('_views/CaiWu/YjingJl')
      },
      {
        path: '/webSetting',
        name: '设置',
        roles: ['普通员工', '大boss'],
        component: () => import('_views/WebSetting')
      }
    ]
  },
  {
    path: '*',
    component: () => import('_views/NotFound')
  },
  {
    path: '/login',
    name: '登录页',
    component: () => import('_views/Login')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 登录鉴权
// 路由前置守卫
// 路由后置守卫  动态添加
router.beforeEach((to, from, next) => {
/*
进行判断是否登录
拦截admin就可以了
 */

  if (to.path !== '/login') {
  // 如果是登录
    if (isLogin()) {
    // 说明登录了
      next()
    } else {
      // 去登陆页
      next('/login')
    }
  } else {
    next()
  }
})
router.afterEach((to, from) => {
  const currest = {
    name: to.name,
    path: to.path
  }
  // 如果是登录页 Layout组件
  const onPaths = ['/login', '/', '/dashBoard']
  if (!onPaths.includes(to.path)) {
    store.commit('nav/ADD_TAB', currest)
  }
})
export default router
